package com.yuanqi.service.impl;

import com.yuanqi.domain.PaymentRecord;
import com.yuanqi.repository.PaymentRecordRepository;
import com.yuanqi.service.PaymentRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaymentRecordServiceImpl implements PaymentRecordService {
    
    @Autowired
    private PaymentRecordRepository paymentRecordRepository;
    
    @Override
    public Page<PaymentRecord> getPaymentRecords(Pageable pageable, String paymentNumber, String orderNumber) {
        if (pageable == null) {
            pageable = PageRequest.of(0, 10);
        }
        
        if (paymentNumber != null || orderNumber != null) {
            List<PaymentRecord> records = getPaymentRecordsByConditions(paymentNumber, orderNumber);
            return Page.empty(pageable);
        }
        
        return paymentRecordRepository.findAll(pageable);
    }
    
    @Override
    public PaymentRecord getPaymentRecordById(Integer id) {
        return paymentRecordRepository.findById(id).orElse(null);
    }
    
    @Override
    public PaymentRecord getPaymentRecordByPaymentNumber(String paymentNumber) {
        return paymentRecordRepository.findByPaymentNumber(paymentNumber).orElse(null);
    }
    
    @Override
    public List<PaymentRecord> getPaymentRecordsByConditions(String paymentNumber, String orderNumber) {
        return paymentRecordRepository.findByConditions(paymentNumber, orderNumber);
    }
    
    @Override
    public Map<String, Object> getPaymentRecordStats() {
        Map<String, Object> stats = new HashMap<>();
        
        try {
            long totalRecords = paymentRecordRepository.count();
            long pendingRecords = paymentRecordRepository.countByStatus(PaymentRecord.STATUS_PENDING);
            long approvedRecords = paymentRecordRepository.countByStatus(PaymentRecord.STATUS_APPROVED);
            long paidRecords = paymentRecordRepository.countByStatus(PaymentRecord.STATUS_PAID);
            
            stats.put("totalRecords", totalRecords);
            stats.put("pendingRecords", pendingRecords);
            stats.put("approvedRecords", approvedRecords);
            stats.put("paidRecords", paidRecords);
            
        } catch (Exception e) {
            stats.put("error", "获取统计数据失败：" + e.getMessage());
        }
        
        return stats;
    }
    
    @Override
    @Transactional
    public PaymentRecord createPaymentRecord(PaymentRecord paymentRecord) {
        paymentRecord.setCreateTime(LocalDateTime.now());
        paymentRecord.setUpdateTime(LocalDateTime.now());
        return paymentRecordRepository.save(paymentRecord);
    }
    
    @Override
    @Transactional
    public PaymentRecord updatePaymentRecord(PaymentRecord paymentRecord) {
        paymentRecord.setUpdateTime(LocalDateTime.now());
        return paymentRecordRepository.save(paymentRecord);
    }
    
    @Override
    @Transactional
    public void deletePaymentRecord(Integer id) {
        paymentRecordRepository.deleteById(id);
    }
}
